package com.yingdong.sceneserver.net;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.yingdong.sceneserver.inner.service.HttpService;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.codec.http.HttpContent;
import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.codec.http.HttpRequest;

public class HttpServerInboundHandler extends ChannelInboundHandlerAdapter {
	private static Log log = LogFactory.getLog(HttpServerInboundHandler.class);

	private HttpRequest request;

	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
		if (msg instanceof HttpRequest) {
			request = (HttpRequest) msg;
			HttpMethod method = request.method();
			String uri = request.uri();
			boolean isGet = method.equals(HttpMethod.GET);
			if (isGet) {
				HttpService.handleHttpRequest(ctx, request);
			}
		}
		if (msg instanceof HttpContent) {
			// HttpContent content = (HttpContent) msg;
			// ByteBuf buf = content.content();
			// System.out.println(buf.toString(io.netty.util.CharsetUtil.UTF_8));
			// buf.release();
			// String res = "I am OK";
			// ctx.writeAndFlush(getResponse(res));
		}
		ctx.disconnect();
	}

	@Override
	public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
		ctx.flush();
	}

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
		ctx.close();
	}

	@Override
	public void channelInactive(ChannelHandlerContext ctx) throws Exception {
		// log.info("断开连接");
	}

}
